﻿using com.guojin.mj.net.message.game;
using com.guojin.mj.net.message.login;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public delegate void RoomIDChangeEventHandler(string roomID);
public delegate void ChapterChangeEventHandler(int currentChapter, int maxChapter);
public delegate void CurrentTotalBetChangeEventHandler(int currentTotalBet);
public delegate void CurrentSingleBetChangeEventHandler(int currentSingleBet);

public class GameRoomModel
{
    private static GameRoomModel instance;

    public static GameRoomModel GetInstance()
    {
        if (instance == null)
        {
            instance = new GameRoomModel();
        }
        return instance;
    }

    public event RoomIDChangeEventHandler RoomIDChangeEvent;
    public event ChapterChangeEventHandler ChapterChangeEvent;
    public event CurrentTotalBetChangeEventHandler CurrentTotalBetChangeEvent;
    public event CurrentSingleBetChangeEventHandler currentSingleBetChangeEvent;

    private  string roomID;
    private int currentChapter;
    private int maxChapter;
    private  int currentTotalBet;
    private  int currentSingleBet;

    public int RoomType { get; set; }
    public string RoomID
    {
        get { return roomID; }
        set
        {
            roomID = value;
            if (RoomIDChangeEvent != null)
            {
                RoomIDChangeEvent(RoomID);
            }
        }
    }
    public int CurrentUserNum { get; set; }
    public int MaxUser { get; set; }
    public int CurrentChapter
    {
        get { return currentChapter; }
        set
        {
            currentChapter = value;
            if (ChapterChangeEvent != null)
            {
                ChapterChangeEvent(CurrentChapter,maxChapter);
            }
        }
    }
    public int MaxChapter
    {
        get { return maxChapter; }
        set
        {
            maxChapter = value;
            if (ChapterChangeEvent != null)
            {
                ChapterChangeEvent(CurrentChapter,MaxChapter);
            }
        }
    }
    public int RoomState { get; set; }
    public int TrusteeshipTime { get; set; }
    public int MaxScore { get; set; }
    public int MaxLimitScore { get; set; }
    public int MinScore { get; set; }

    public bool isfinishGame { get; set; }
    public int CurrentTotalBet
    {
        get { return currentTotalBet; }
        set
        {
            currentTotalBet = value;
            if (CurrentTotalBetChangeEvent != null)
            {
                CurrentTotalBetChangeEvent(CurrentTotalBet);
            }
        }
    }
    public int CurrentSingleBet
    {
        get { return currentSingleBet; }
        set
        {
            currentSingleBet = value;
            if (currentSingleBetChangeEvent != null)
            {
                currentSingleBetChangeEvent(CurrentSingleBet);
            }
        }
    }
    public bool IsJvGuo { get; set; }

    public  void SaveRoomInfoToGameRoomModel(int roomType, string roomID, int currentUserNum, int maxUser,
int currentChapter, int maxChapter, int roomState, int trusteeshipTime)
    {
        RoomType = roomType;
        RoomID = roomID;
        CurrentUserNum = currentUserNum;
        MaxUser = maxUser;
        CurrentChapter = currentChapter;
        MaxChapter = maxChapter;
        RoomState = roomState;
        TrusteeshipTime = trusteeshipTime;
        MaxScore = 0;
        MaxLimitScore = 0;
        MinScore = 0;
        CurrentTotalBet = 0;
        currentSingleBet = 0;
        IsJvGuo = false;
    }

    //玩家的dic
    public Dictionary<int, Player> playerDic = new Dictionary<int, Player>();

    public void GameStartAppendPlayersToPlayerDic(List<GameUserInfoRet> gameUser)
    {
        playerDic.Clear();

        for (int i = 0; i < gameUser.Count; i++)
        {
            TooL.LoadPrefab(GameRoomPage.GetInstance().loadPointGroup[GameController.GetInstance().intArr[gameUser[i].locationIndex]].
                gameObject.transform.FindChild("headPoint").gameObject, "playerHead");

            TooL.LoadPrefab(GameRoomPage.GetInstance().loadPointGroup[GameController.GetInstance().intArr[gameUser[i].locationIndex]].
              gameObject.transform.FindChild("cardPoint").gameObject, "playerCard");

            Player player = GameRoomPage.GetInstance().loadPointGroup[GameController.GetInstance().intArr[gameUser[i].locationIndex]].
                gameObject.transform.FindChild("headPoint/playerHead"). gameObject.AddComponent<Player>();

            playerDic.Add(gameUser[i].locationIndex, player);

            player.InitPlayerUserInfo(gameUser[i]);                                                                                                                                                      // 初始化头像
        }
    }

    public void AppendPlayerToPlayerDic(int locationIndex, Player player)
    {
        playerDic.Add(locationIndex, player);
    }

    public void RemovePlayerFromPlayerDic(int locationIndex)
    {
        GetInstance().playerDic.Remove(locationIndex);
    }

    public Player GetPlayerFromPlayerDic(int locationIndex)
    {
        Player player = playerDic[locationIndex];
        return player;
    }

    //同步单注数
    public void AppendSingleBet(int num)
    {
        CurrentSingleBet = num;
    }

    public int localstate;//玩家自己的游戏状态
}
